import $ from 'jquery';
import Button from './Button';

export default class TitleButton extends Button {
  _init() {
    this.menu = [{
      name: 'normal',
      text: this._t('normalText'),
      param: 'p'
    }, '|', {
        name: 'h1',
        text: `${this._t('title')} 1`,
        param: 'h1'
      }, {
        name: 'h2',
        text: `${this._t('title')} 2`,
        param: 'h2'
      }, {
        name: 'h3',
        text: `${this._t('title')} 3`,
        param: 'h3'
      }, {
        name: 'h4',
        text: `${this._t('title')} 4`,
        param: 'h4'
      }, {
        name: 'h5',
        text: `${this._t('title')} 5`,
        param: 'h5'
      }];
    return super._init(this);
  }

  setActive(active, param) {
    super.setActive.call(this, active);
    if (active) {
      param || (param = this.node[0].tagName.toLowerCase());
    }
    this.el.removeClass('active-p active-h1 active-h2 active-h3 active-h4 active-h5');
    if (active) {
      return this.el.addClass(`active active-${param}`);
    }
  }

  command(param) {
    let $rootNodes;
    $rootNodes = this.editor.selection.rootNodes();
    this.editor.selection.save();
    $rootNodes.each(((_this => (i, node) => {
      let $node;
      $node = $(node);
      if ($node.is('blockquote') || $node.is(param) || $node.is(_this.disableTag) || _this.editor.util.isDecoratedNode($node)) {
        return;
      }
      return $(`<${param}/>`).append($node.contents()).replaceAll($node);
    }))(this));
    this.editor.selection.restore();
    return this.editor.trigger('valuechanged');
  }
}

TitleButton.prototype.name = 'title';
TitleButton.prototype.htmlTag = 'h1, h2, h3, h4, h5';
TitleButton.prototype.disableTag = 'pre, table';